package com.bmd.backgroundmanger.controller;

import com.bmd.backgroundmanger.commons.Result;
import com.bmd.backgroundmanger.entity.UserFavorite;
import com.bmd.backgroundmanger.service.IUserFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收藏Controller
 */
@RestController
@RequestMapping("/userFavorite")
@Tag(name = "用户收藏")
public class UserFavoriteController {

    @Resource
    private IUserFavoriteService userFavoriteService;

    @PostMapping("/addFavorite")
    @Operation(summary = "添加收藏")
    public Result<String> addFavorite(@RequestParam Integer userId, @RequestParam Integer questionId) {
        boolean success = userFavoriteService.addFavorite(userId, questionId);
        if (success) {
            return Result.success("收藏成功!");
        } else {
            return Result.error(400, "已经收藏过该题目!");
        }
    }

    @DeleteMapping("/removeFavorite")
    @Operation(summary = "取消收藏")
    public Result<String> removeFavorite(@RequestParam Integer userId, @RequestParam Integer questionId) {
        boolean success = userFavoriteService.removeFavorite(userId, questionId);
        if (success) {
            return Result.success("取消收藏成功!");
        } else {
            return Result.error(400, "取消收藏失败!");
        }
    }

    @GetMapping("/getUserFavorites/{userId}")
    @Operation(summary = "获取用户收藏列表")
    public Result<List<UserFavorite>> getUserFavorites(@PathVariable Integer userId) {
        List<UserFavorite> favorites = userFavoriteService.getUserFavorites(userId);
        return Result.success(favorites);
    }

    @GetMapping("/checkFavorite")
    @Operation(summary = "检查是否已收藏")
    public Result<Boolean> checkFavorite(@RequestParam Integer userId, @RequestParam Integer questionId) {
        boolean isFavorite = userFavoriteService.isFavorite(userId, questionId);
        return Result.success(isFavorite);
    }
} 